package net.jltec.view.model;

import java.util.HashMap;
import java.util.Map;

import net.jltec.config.AppConfig;
import net.jltec.utils.CallBackMethod;
import net.jltec.utils.MD5;
import net.jltec.utils.ProcessMethod;
import net.jltec.utils.WebUtils;
import net.jltec.view.JltecMainUI;
import net.jltec.view.R;
import net.jltec.view.base.JltecBaseUI;
import net.jltec.view.bean.User;

import org.apache.commons.lang.StringUtils;

import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;

@SuppressWarnings({"unchecked","rawtypes"})
public class JltecLoginUI extends JltecBaseUI implements OnClickListener {
	
	private Button loginBtn ;
	private EditText userNameText;
	private EditText userPassText;
	private CheckBox autologinBox;
	private CheckBox remPassWordBox;

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 声明使用自定义标题
		setContentView(R.layout.login);
		initParams();
		
		 
	}
	
	private void initParams(){
		userNameText = (EditText) findViewById(R.id.userName);
		userPassText = (EditText) findViewById(R.id.userPass);
		autologinBox = (CheckBox) findViewById(R.id.autologin);
		remPassWordBox = (CheckBox) findViewById(R.id.remPassWord);
		
		
		loginBtn = (Button) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(this);
		
		
		boolean autologin = false;//configs.getBoolean(User.AUTO_LOGIN, false);
		autologinBox.setChecked(autologin);
		
		boolean remPassword = configs.getBoolean(User.REM_PASSWORD, false);
		remPassWordBox.setChecked(remPassword);
		
		String username = configs.getString(User.USERNAME, "");
		String password = configs.getString(User.PASSWORD, "");
		
		if(remPassword){
			userNameText.setText(username);
			userPassText.setText(password);
		}
		
		
		if(autologin){
			login(username, password);
		}
		
	}

	@Override
	public void onClick(View v) {
		if(v==loginBtn){
			String username = userNameText.getText().toString();
			String noMd5Password=  userPassText.getText().toString();
			
			String password = MD5.get16Str(noMd5Password);
			
			final Editor edit = configs.edit();
			edit.putBoolean(User.AUTO_LOGIN, autologinBox.isChecked());
			edit.putBoolean(User.REM_PASSWORD, remPassWordBox.isChecked());
			
			edit.putString(User.USERNAME, username);
			edit.putString(User.PASSWORD, noMd5Password);
			edit.commit();
			
			login(username,password);
			
		}
	}
	
	
	private void login(final String username,final String password){
		if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
			showToast("用户名或密码能不空!");
			return;
		}
		
		asynMethod("正在登录...",new ProcessMethod() {
			@Override
			public Object execute() {
				Map p = new HashMap();
				
				p.put("username", username);
				p.put("password",password);
				String json = WebUtils.doPost(AppConfig.LOGIN, p);
				Map rs = JSON.parseObject(json, Map.class);
				return rs;
			}
		}, new CallBackMethod() {
			@Override
			public void execute(Object result) {
				
				Map params = (Map) result;
				
				if(params!=null){
					
					int manid= convert(params.get("manid"),Integer.class);
					
					if(manid>0){
						String name = convert(params.get("name"),String.class);
						String password=convert(params.get("pass"),String.class);
						String oauthflag=(name+password);
						final Editor edit = configs.edit();
						edit.putString(User.OAUTH_TOKEN_SECRET, oauthflag);
						edit.commit();
						Intent i = new Intent(JltecLoginUI.this, JltecMainUI.class);
						startActivity(i);
						finish();
					}else{
						showToast("登陆失败!");
					}
				}
			}
		});
	}
}
